#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>

int main()
{
	pid_t id = fork();
	if(id<0)
	{
		printf("子进程创建失败！\n");
	}
	else if(id == 0)
	{
		//子进程
		sleep(3);
		execl("/usr/bin/ls","ls","-a","-l","-n",NULL);
		sleep(1);
	}
	else
	{
		//父进程
		while(1)
		{
			pid_t rid = waitpid(id,NULL,WNOHANG);
			if(rid == id)
			{
				printf("wait child success!\n");
				sleep(1);
				break;
			}
			else if(rid == 0)
			{
				printf("child proc. is still running!\n");
				sleep(1);
				continue;
			}
			else
			{
				perror("waitpid()");
				exit(EXIT_FAILURE);
			}
	        } 
	}

	return 0;
}

